package com.iot.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.iot.commons.ResponseResult;
import com.iot.enums.StatusEnum;
import com.iot.mapper.MessageMapper;
import com.iot.model.dto.ShareDto;
import com.iot.model.entity.MessageEntity;
import com.iot.model.query.MessageQuery;
import com.iot.model.dto.MessageDto;
import com.iot.model.vo.AuthInfoVo;
import com.iot.model.vo.MessageVo;
import com.iot.service.MessageService;
import com.iot.service.ShareService;
import com.iot.utils.DateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.iot.utils.ResponseResultUtil;

import java.util.List;

/**
 * MessageServiceImpl
 *
 * @author: dh
 * @date: 2024-04-28 16:12:50
 */
@Service
@RequiredArgsConstructor
public class MessageServiceImpl implements MessageService {

    private final ShareService shareService;

    private final MessageMapper messageMapper;

    @Override
    public ResponseResult getList(MessageQuery message) {
        AuthInfoVo authInfoEntity = (AuthInfoVo) StpUtil.getSession().get("loginInfo");
        if (null == authInfoEntity) {
            return ResponseResultUtil.authInfoFailure();
        }

        LambdaQueryWrapper<MessageEntity> queryWrapper = Wrappers.lambdaQuery();
        if (null != message.getStartTime()) {
            queryWrapper.ge(MessageEntity::getCreateTime, message.getStartTime());
        }
        queryWrapper.eq(MessageEntity::getState, StatusEnum.VALID.getKey());
        if (null == message.getIsRead()) {
            queryWrapper.eq(MessageEntity::getIsRead, 1);
        } else {
            if (!message.getIsRead().equals(2)) {
                queryWrapper.eq(MessageEntity::getIsRead, message.getIsRead());
            }
        }

        queryWrapper.eq(MessageEntity::getAuthId, authInfoEntity.getId());
        queryWrapper.orderByDesc(MessageEntity::getCreateTime);
        List<MessageEntity> messageEntities = messageMapper.selectList(queryWrapper);
        List<MessageVo> messageVoList = Lists.newArrayList();
        List<Long> readList = Lists.newArrayList();
        for (MessageEntity messageEntity : messageEntities) {
            MessageVo vo = new MessageVo();
            readList.add(messageEntity.getId());
            vo.setId(messageEntity.getId());
            vo.setTitle(messageEntity.getTitle());
            vo.setContent(messageEntity.getContent());
            vo.setOtherParams(messageEntity.getOtherParams());
            vo.setType(messageEntity.getType());
            vo.setCreateTime(messageEntity.getCreateTime());
            vo.setIsRead(messageEntity.getIsRead());
            messageVoList.add(vo);
        }

        if (CollectionUtils.isNotEmpty(readList)) {
            LambdaUpdateWrapper<MessageEntity> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.in(MessageEntity::getId, readList);
            MessageEntity messageEntity = new MessageEntity();
            messageEntity.setIsRead(0);
            messageMapper.update(messageEntity, updateWrapper);
        }

        return ResponseResultUtil.success(messageVoList);
    }

    @Override
    public ResponseResult reply(MessageDto message) {
        if (null == message) {
            return ResponseResultUtil.paramFailure();
        }
        Long id = message.getId();
        if (null == id) {
            return ResponseResultUtil.paramFailure();
        }
        MessageEntity messageEntity = messageMapper.selectById(id);
        if (null == messageEntity) {
            return ResponseResultUtil.failure();
        }
        ShareDto share = new ShareDto();
        share.setId(messageEntity.getSid());
        share.setAccept(message.getReplyType());
        share.setUid(messageEntity.getUid());
        share.setMessageId(id);
        share.setLanguage(message.getLanguage());
        return shareService.reply(share);
    }

    @Override
    public ResponseResult add(MessageDto message) {
        MessageEntity entity = new MessageEntity();
        entity.setTitle(message.getTitle());
        entity.setContent(message.getContent());
        entity.setType(message.getType());
        entity.setAuthId(message.getAuthId());
        entity.setUid(message.getUid());
        entity.setIsRead(1);
        entity.setSid(message.getSid());
        entity.setState(StatusEnum.VALID.getKey());
        entity.setCreateTime(DateUtil.nowSecond());
        messageMapper.insert(entity);
        return ResponseResultUtil.success();
    }

    @Override
    public ResponseResult getById(MessageQuery message) {
        MessageEntity messageEntity = messageMapper.selectById(message.getId());
        MessageVo vo = new MessageVo();
        vo.setId(messageEntity.getId());
        vo.setTitle(messageEntity.getTitle());
        vo.setContent(messageEntity.getContent());
        vo.setType(messageEntity.getType());
        return ResponseResultUtil.success(vo);
    }
}
